package com.zking.myservlet.user;

import com.zking.dao.UserDao;
import com.zking.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Calendar;

@WebServlet("/manage/userUpdate")
public class UserUpdate extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 处理请求的编码，解决中文乱码
        request.setCharacterEncoding("utf-8");
        // 处理响应的编码，解决中文乱码
        response.setContentType("text/html;charset=utf-8");

        System.out.println("UserUpdate===进来");

        int pageIndex = Integer.parseInt(request.getParameter("pageIndex")); // 假设从页面传入了 pageIndex 参数

        String UserId= request.getParameter("userName");
        String UserName= request.getParameter("name");
        String Password= request.getParameter("passWord");
        String Sex= request.getParameter("sex");
        // 处理出生日期
        String birthYear = request.getParameter("birthyear");
        String birthMonth = request.getParameter("birthmonth");
        String birthDay = request.getParameter("birthday");

        String Birthday = birthYear + "-" + birthMonth + "-" + birthDay;

        String IdentityCode= request.getParameter("identityCode");
        String Email= request.getParameter("email");
        String Mobile= request.getParameter("mobile");
        String Address= request.getParameter("address");
        // 假设还有其他需要更新的用户信息，可以继续在这里获取

        // 创建一个新的用户对象
        User user = new User(UserId, UserName, Password,Sex,Birthday,IdentityCode,Email,Mobile,Address,0);

        // 调用 UserDao 中的方法来更新用户信息
        boolean flag = UserDao.updateUser(user);

        if (flag) {
            response.sendRedirect("userSelectByPage?pageIndex=" + pageIndex);
        } else {
            // 处理更新失败的情况，例如返回一个错误页面或者错误信息
            response.getWriter().println("更新用户信息失败");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
